package com.example.myapplication;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * @author: rongfeng
 * @date: 2021/1/26
 * @description
 */
public class LeeksFound {
    static volatile int index = 0;

    public static void main(String[] args) {
        next();
    }

//    public static String[] strings = new String[]{"007460","040015","519778","005001","006527","002593","003293","001216"};

    public static String[] strings = new String[]{ "000592","166009","002593","163409","001667","001216","003293","006527"};

//

    //"010330","320003","110012","166005","000547"



//    public static String[] strings = new String[]{"163807"};


    private static void next() {
        if (index < strings.length) {
            System.out.println("index " + index);
            String url = String.format("http://fund.eastmoney.com/%s.html?spm=search", strings[index]);
            index++;
            getDns(url);
        } else {
            fenxi();
        }
    }


    private static void fenxi() {

        float total = 0;
        List<Bean> list = new ArrayList<>();

        for (Bean bean1 : mList) {
            Bean bean = null;
            for (Bean bean2 : list) {
                if (bean2.name.equals(bean1.name)) {
                    bean = bean2;
                    break;
                }
            }
            if (bean == null) {
                bean = new Bean();
                bean.percent = bean1.percent;
                list.add(bean);
            }
            bean.name = bean1.name;
            bean.totalPercent = bean.totalPercent + bean1.percent;
            bean.count++;
            bean.foundName.addAll(bean1.foundName);
            total = total + bean1.percent;
        }


        for (int i = 0; i < list.size(); i++) {
            for (int j = i + 1; j < list.size(); j++) {
                float p1 = list.get(i).totalPercent;
                float p2 = list.get(j).totalPercent;
                if (p2 > p1) {
                    list.add(i, list.get(j));
                    list.remove(j + 1);
                }
            }
        }

        for (int i = 0; i < strings.length; i++) {
            System.out.print(strings[i] + " ");
        }
        System.out.println("");
        System.out.println("共" + list.size() + "只股票");

        for (Bean bean : list) {
            BigDecimal bd = new BigDecimal(bean.totalPercent / total * 100);
            bd = bd.setScale(2, RoundingMode.HALF_UP);
            System.out.println(bean.name + " : " + bd.toString() + " %");
            System.out.println(bean.count + " 只基");
            for (String foundName : bean.foundName) {
                System.out.println(foundName);
            }
            System.out.println("\n");
        }


    }

    static volatile List<Bean> mList = new ArrayList<>();


    public static void getDns(String url) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .build();
        final Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, @NotNull okhttp3.Response response) throws IOException {
                if (response.body() != null) {
                    String html = response.body().string();
                    String foundName = jiexi2(html);
                    jiexi(html, foundName);
                    next();
                }
            }
        });
    }

    //可以闹情绪，但忠诚和责任要兑现
    private static String jiexi2(String html) {
        String tag1 = "<title>";
        int index1 = html.indexOf(tag1);
        String html1 = html.substring(index1 + tag1.length());
        String tag2 = "基金净值";
        int index2 = html1.indexOf(tag2);
        String html2 = html1.substring(0, index2);
        return html2;
    }

    private static void jiexi(String html, String foundName) {
        String tag1 = "<td class=\"alignLeft\">";
        int index1 = html.indexOf(tag1);
        if (index1 > 0) {
            Bean bean = new Bean();
            String html1 = html.substring(index1 + tag1.length());
            String tag2 = "title=\"";
            int index2 = html1.indexOf(tag2);
            String html2 = html1.substring(index2 + tag2.length());
            String tag3 = "\">";
            int index3 = html2.indexOf(tag3);
            String html3 = html2.substring(0, index3);
            bean.name = html3;
            bean.foundName.add(foundName);
            String tag4 = "class=\"alignRight bold\">";
            int index4 = html2.indexOf(tag4);
            String html4 = html2.substring(index4 + tag4.length());

            String tag5 = "%</td>";
            int index5 = html4.indexOf(tag5);
            if (index5 > 0) {
                String html5 = html4.substring(0, index5);
                if (html5.length() < 6) {
                    bean.percent = Float.parseFloat(html5);
                    mList.add(bean);
                    jiexi(html4, foundName);
                }
            }
        }

    }

    public static class Bean {
        public Set<String> foundName = new HashSet<>();
        public String name;
        public float percent;
        public int count;
        public float totalPercent;
    }
}
